//
//  ChangePswViewController.m
//  Jingjing
//
//  Created by user on 15/11/9.
//  Copyright © 2015年 Mikeshihua. All rights reserved.
//

#import "ChangePswViewController.h"
#import "LoginViewController.h"
@interface ChangePswViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
@property(nonatomic,strong)UITableView  *changePswTab;
@property(nonatomic,strong) UIButton*changePswBtn ;
@end

@implementation ChangePswViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:239.0f/255.0f green:239.0f/255.0f blue:239.0f/255.0f alpha:1.0f];
    TopView *topView = [[TopView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    [topView setTitile:@"重置密码"];
    [self.view addSubview:topView];
    
    UIButton * backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10.0f, 20.0f, 44.0f, 44.0f)];
    [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnCLick:) forControlEvents:UIControlEventTouchUpInside];
    backBtn.titleLabel.textAlignment = NSTextAlignmentRight;
    [topView addSubview:backBtn];
    [self createUI];
}
- (void)backBtnCLick:(UIButton*)btn
{
    [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)createUI
{
    
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardhide)];
    tapGesture.cancelsTouchesInView = NO;
    [self.changePswTab addGestureRecognizer:tapGesture];
    
    
    
    [self.view addSubview:self.changePswTab];
    [self.changePswTab addSubview:self.changePswBtn];
}
-(UITableView *)changePswTab
{
    if (!_changePswTab) {
        
        _changePswTab = [[UITableView alloc] initWithFrame:CGRectMake(16.0f, 64.0f+16.0f, SCREEN_WIDTH-32.0f, SCREEN_HEIGHT-64.0f-16.0f) style:UITableViewStylePlain];
        _changePswTab.delegate = self;
        _changePswTab.dataSource = self;
        _changePswTab.rowHeight = 60.0f;
        _changePswTab.backgroundColor = [UIColor clearColor];
        _changePswTab.separatorStyle = UITableViewCellSeparatorStyleNone;

        
    }
    return _changePswTab;
}
//下一步按钮
-(UIButton *)changePswBtn
{
    if (!_changePswBtn) {
        _changePswBtn = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-32.0f)/2-100.0f/2,212.0f, 100.0f, 44.0f)];
        [_changePswBtn addTarget:self action:@selector(changePswBtnTapped) forControlEvents:UIControlEventTouchUpInside];
        [_changePswBtn setTitle:@"确认" forState:UIControlStateNormal];
        _changePswBtn.layer.cornerRadius = 5;
        _changePswBtn.layer.masksToBounds = YES;
        _changePswBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14.0f];
        [_changePswBtn setBackgroundColor:kColorOrangeNormal forState:UIControlStateNormal];
        [_changePswBtn setBackgroundColor:kColorOrangePressed forState:UIControlStateHighlighted];
        [_changePswBtn setBackgroundColor:kColorOrangeDisabled forState:UIControlStateDisabled];
        _changePswBtn.enabled = NO;
        
        
    }
    return _changePswBtn;
}
- (void)changePswBtnTapped
{
    //重置密码
    
    if (![__textFieldPwd.text isEqualToString:__textFieldConfirmPwd.text]) {
        ALERT_VIEW(@"新密码和确认密码不一致");
        return;
    }
    
    if ([AccountTools validatePassWord:__textFieldPwd.text]==NO) {
        ALERT_VIEW(@"密码为8~32位字符");
        return;
    }
    [[AnimationIndicator sharedIndicator] startAnimationWithLoadText:@"加载中..."];
    [AppGlobalClass PostDataWithNSstring:UPDATEUSERPASSWORD withDictionary:@{@"token":[AccountTools getUserToken],@"oldPassword":__textField.text,@"newPassword":__textFieldPwd.text} success:^(NSDictionary *dictionary) {
        
        
        
        if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
        {
            
            ALERT_VIEW(@"密码重置成功");
            
            [self.navigationController popViewControllerAnimated:TRUE];
            
            
        }else
        {
            NSString * str = [dictionary objectForKey:@"message"];
            
            ALERT_VIEW(str);
            
        }
        
        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    } failure:^(NSError *error) {
        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
        
    } networkerror:^(NSString *networkerror) {
        
        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    }];
    
}

- (void)keyboardhide
{
    [__textField resignFirstResponder];
    [__textFieldPwd resignFirstResponder];
    [__textFieldConfirmPwd resignFirstResponder];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
static UITextField *__textField;//记录当前旧密码的textField
static UITextField *__textFieldPwd;//记录当前新密码的textField
static UITextField *__textFieldConfirmPwd;//记录当前确认密码的textField
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIView * separtor = [[UIView alloc]initWithFrame:CGRectMake(0.0f, 59.5f, SCREEN_WIDTH - 32.0f, 0.5)];
        separtor.backgroundColor =  [UIColor hexStringToColor:@"#bdbdbd"];
        separtor.tag = 111;
        [cell.contentView addSubview:separtor];//分割线
        
        UILabel *userlabel = [[UILabel alloc]initWithFrame:CGRectMake(24.0f, 16.0f, 100.0f, 28.0f)];
        userlabel.tag = 1000;
        userlabel.font = [UIFont systemFontOfSize:16.0f];
        userlabel.textColor = kColorTEXT;
        [cell.contentView addSubview:userlabel];
        
        UITextField *userTF = [[UITextField alloc] init];
        userTF.tag = 2000;
        userTF.delegate = self;
        userTF.secureTextEntry = YES;
        userTF.clearButtonMode = UITextFieldViewModeWhileEditing;
        [userTF setValue:[UIFont boldSystemFontOfSize:14.0f] forKeyPath:@"_placeholderLabel.font"];
        userTF.font = [UIFont systemFontOfSize:16.0f];
        userTF.textColor = kColorTEXT;
        userTF.frame = CGRectMake(130.0f, 16.0f, SCREEN_WIDTH-154.0f-32.0f, 28.0f);
        [userTF addTarget:self action:@selector(textFieldTapped:) forControlEvents:UIControlEventEditingChanged];
        [cell.contentView addSubview:userTF];
        
        
    }
    
    UILabel *userLabel = (UILabel*)[cell.contentView viewWithTag:1000];
    UITextField *text = (UITextField *)[cell.contentView viewWithTag:2000];
    UIView *separtor = (UIView*)[cell.contentView viewWithTag:111];
    if (indexPath.row==2) {
        separtor.hidden = YES;
    }else{
        separtor.hidden = NO;
    }

    if (indexPath.row==0) {
        userLabel.text = @"原密码";
        text.placeholder = @"请输入您的旧密码";
        __textField = text;
        
    }else if (indexPath.row==1){
        
        userLabel.text = @"新密码";
        text.placeholder = @"请输入您的新密码";
        __textFieldPwd = text;

    }else{
        userLabel.text = @"确认密码";
        text.placeholder = @"请再次输入新密码";
        __textFieldConfirmPwd = text;
    }
    
    return cell;
}

- (void)textFieldTapped:(id)sender
{
    
    
    
    if ([AccountTools isEmpty:__textField.text]==NO && [AccountTools isEmpty:__textFieldPwd.text]==NO&&[AccountTools isEmpty:__textFieldConfirmPwd.text]==NO) {
        self.changePswBtn.enabled = YES;
        
    }else{
       
        self.changePswBtn.enabled = NO;
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self keyboardhide];
    return YES;
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloginView) name:@"reLogin" object:nil];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(void)reloginView
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"账号在另一台机器登录，请重新登录!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
}
#pragma mark------------alertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //删除token等用户数据
    [AccountTools exitLogin];
    LoginViewController *loginViewCon = [[LoginViewController alloc]init];
    loginViewCon.backBtHide = FALSE;
    [self presentViewController:loginViewCon animated:TRUE completion:^{
        //回到哪个界面
    }];
    
}

@end
